#include "world.h"

World::World()
{
	height = 2200;
	spPoc = 0;
	imgBackground.LoadFromFile("data/background.png");
	imgBackground.SetSmooth(false);
	imgClouds[0].LoadFromFile("data/c1.png");
	imgClouds[1].LoadFromFile("data/c2.png");
	imgClouds[2].LoadFromFile("data/c3.png");
	imgClouds[3].LoadFromFile("data/c4.png");
        imgClouds[0].SetSmooth(false);
        imgClouds[1].SetSmooth(false);
        imgClouds[2].SetSmooth(false);
        imgClouds[3].SetSmooth(false);	
	imgGround.LoadFromFile("data/ground.png");
	imgGround.SetSmooth(false);
	spBackground.SetImage(imgBackground);
	spBackground.SetPosition(0.0f, 0.0f);
	spGround.SetImage(imgGround);
	spGround.SetPosition(0.0f, height - 344);
	for (int i = 0; i < 5; i++)
	{
		if (sf::Randomizer::Random(0, 9) < 4)
		{
			spClouds[0][i].SetImage(imgClouds[sf::Randomizer::Random(0, 3)]);
			int x = sf::Randomizer::Random(-200, 100);
			int y = (i + 1) * 400 + sf::Randomizer::Random(-60, 60) - 200;
			spClouds[0][i].SetPosition(x, y);
			spClouds[1][i].SetImage(imgClouds[sf::Randomizer::Random(0, 3)]);
			x = sf::Randomizer::Random(300, 600);
			y = (i + 1) * 400 + sf::Randomizer::Random(-60, 60) - 200;
			spClouds[1][i].SetPosition(x, y);
		}
		else
		{
			spClouds[0][i].SetImage(imgClouds[sf::Randomizer::Random(0, 3)]);
                        int x = sf::Randomizer::Random(-150, 600);
                        int y = (i + 1) * 400 + sf::Randomizer::Random(-60, 60) - 200;
                        spClouds[0][i].SetPosition(x, y);
		}
	}
}

void World::update(int newHeight)
{
	if (newHeight < 400) newHeight = 400;
	for (int i = 0; i < 6; i++)
	{
		spClouds[0][i].SetY(spClouds[0][i].GetPosition().y - (height - newHeight));
                spClouds[1][i].SetY(spClouds[1][i].GetPosition().y - (height - newHeight));		
	}
	height = newHeight;
	spGround.SetY(height - 344);
}

sf::Sprite* World::getSprite()
{
	switch(spPoc++)
	{
	case 0: return &spBackground; break;
	case 1: return &spClouds[0][0]; break;
        case 2: return &spClouds[1][0]; break;
        case 3: return &spClouds[0][1]; break;
        case 4: return &spClouds[1][1]; break;
        case 5: return &spClouds[0][2]; break;
        case 6: return &spClouds[1][2]; break;
        case 7: return &spClouds[0][3]; break;
        case 8: return &spClouds[1][3]; break;
        case 9: return &spClouds[0][4]; break;
        case 10: return &spClouds[1][4]; break;
	case 11: return &spGround; break;
	case 12: spPoc = 0; return NULL; break;
	}	
}

int World::getHeight()
{
	return height;
}

World::~World()
{
}
